﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace PaulBeck.Timesheet.WebParts.BookTime
{
    public partial class BookTimeUserControl : UserControl
    {

        #region "Properties"
 
        public string NewBookTimeItemUrl
        {
            get
            {
                return CommonWFE.GetTimesheetEditUrl();
            }
        }

        public int WeekId
        {
            get
            {
                object o = ViewState["WeekId"];
                return (o == null) ? 0 : (int)o;
            }
            set
            {
                ViewState["WeekId"] = value;
            }
        }

        public string CurrentUserName
        {
            get
            {
                return CommonWFE.CurrentUserName();
            }
        }

        #endregion

        #region "Control Events"

        protected void Page_Load(object sender, EventArgs e)
        {
 
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            // Update the Status of the timesheet
            BOL.Common.UpdateWeekEntries(BOL.Status.Submitted, WeekId, CurrentUserName);
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            // Update the Status of the timesheet
            BOL.Common.UpdateWeekEntries(BOL.Status.Approved, WeekId, CurrentUserName);
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            // Update the Status of the timesheet
            BOL.Common.UpdateWeekEntries(BOL.Status.Rejected, WeekId, CurrentUserName);
        }

        #endregion

        public void DisplayBookTimeSummary(int weekid)
        {
            WeekId = weekid;
            BOL.BookTimeBOL bt = new BOL.BookTimeBOL();
            var times = bt.GetBookTimes(weekid);

            StringBuilder sb = new StringBuilder();
            sb.Append("<table border=\"0\" class=\"ts-tbl-booktime\">");
            sb.Append("<tr class=\"ts-tbl-booktime-th\"><td class=\"ts-tbl-bt-th-proj\">Project</td><td class=\"ts-tbl-bt-th-note\">Notes</td><td class=\"ts-tbl-bt-th-hour\">Hours</td><td  class=\"ts-tbl-bt-th-acti\">Actions</td></tr>");
            string prevItemDate = null;
            double dayhrs = 0;
            double weekhrs = 0;
            int count = 0;
            string[] listTimeCalc = new string[7];

            foreach (var item in times)
            {
                if (item.Date.ToShortDateString() != prevItemDate)
                {
                    sb.AppendFormat("<tr class=\"ts-tbl-date-b\"><td colspan=\"2\">{0}</td><td class=\"ts-tbl-date-b-time\">count{1}</td><td>&nbsp;</td></tr>", item.Date.ToString("D", CultureInfo.CreateSpecificCulture("en-UK")), count.ToString());
                    listTimeCalc[count] = dayhrs.ToString();
                    dayhrs = 0;
                    count++;
                }
                sb.AppendFormat("<tr><td class=\"ts-tbl-row-td\">{0}</td><td>{1}</td><td class=\"ts-tbl-row-td-hr\">{2}</td><td class=\"ts-tbl-row-td-edit\"><a href=\"javascript:OpenDialogAdr('{5}?IsDlg=1&rptid={3}&weekid={4}')\">Edit task</a></td></tr>", item.Project, item.Notes, item.Hours, item.Id, WeekId, CommonWFE.GetTimesheetEditUrl());
                dayhrs = dayhrs + (double)item.Hours;                  //  Add row hours together
                weekhrs = weekhrs + (double)item.Hours;
                prevItemDate = item.Date.ToShortDateString();                // Set the last item seens date
            }
            sb.AppendFormat("<tr class=\"ts-tbl-total-b\"><td colspan=\"2\">Total</td><td class=\"ts-tbl-total-hr\">{0}</td><td>&nbsp;</td></tr>", weekhrs.ToString());
            sb.Append("</table>");
            listTimeCalc[count] = dayhrs.ToString();

            for (int i = 0; i < count; i++)
            {
                sb.Replace("count" + i.ToString(), listTimeCalc[i+1].ToString());
            }
            litTimesheet.Text = sb.ToString();
            EnableButtons();
        }

        #region "Private Methods"

        private void EnableButtons()
        {
            BOL.Status weekStatus = BOL.Common.GetWeekStatus(WeekId);
            if (weekStatus == BOL.Status.Missing || weekStatus == BOL.Status.Unsubmitted || weekStatus == BOL.Status.Rejected)
            {
                btnSubmit.Enabled = true;
                btnApprove.Visible = false;
                btnReject.Visible = false;
            }
            else
            {
                btnSubmit.Enabled = false;
                if (SPContext.Current.Web.CurrentUser.IsSiteAdmin)
                {
                    btnApprove.Visible = true;
                    btnReject.Visible = true;
                }
            }
        }

        #endregion

    }
}
